(* Content-type: application/vnd.wolfram.mathematica *)

(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)

(* CreatedBy='Mathematica 9.0' *)

(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[       157,          7]
NotebookDataLength[     31493,        805]
NotebookOptionsPosition[     28756,        760]
NotebookOutlinePosition[     29300,        780]
CellTagsIndexPosition[     29257,        777]
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{
Cell[BoxData[
 RowBox[{"ClearAll", "[", "\"\<Global`*\>\"", "]"}]], "Input",
 CellChangeTimes->{{3.6156636662798567`*^9, 3.615663666284843*^9}},
 CellLabel->"In[43]:=",ExpressionUUID->"f25518a5-68eb-46fb-9d4c-b9b0fd448da3"],

Cell["\<\
Calibration of simulated model following Di Giovanni and Levchenko (2012)\
\>", "Text",
 CellChangeTimes->{{3.900753136963245*^9, 
  3.9007531655342536`*^9}},ExpressionUUID->"8f344a9c-ebb5-49d0-98ba-\
081018caf2bf"],

Cell[BoxData[{
 RowBox[{
  RowBox[{"\[Epsilon]", "=", "6"}], ";", 
  RowBox[{"b", "=", "0.1"}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"fp", "=", "1.054671"}], ";", 
   RowBox[{"fe", "=", "1.91893"}], ";"}], 
  "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"labor", "=", 
   RowBox[{"155", "*", 
    SuperscriptBox["10", "6"]}]}], ";", 
  RowBox[{"\[Beta]", "=", "0.65"}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"tail", "=", "1.06"}], ";", 
   RowBox[{"\[Theta]", "=", 
    RowBox[{"tail", "*", 
     RowBox[{"(", 
      RowBox[{"\[Epsilon]", "-", "1"}], ")"}]}]}], ";"}], 
  "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"\[Sigma]", "=", "0.12"}], ";", 
  RowBox[{"A", "=", "1"}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"USvol", "=", "0.0238"}], ";"}]}], "Input",
 CellChangeTimes->{{3.8348434045179367`*^9, 3.8348434094995356`*^9}, {
  3.834849404144765*^9, 3.8348494432883115`*^9}},
 CellLabel->
  "In[205]:=",ExpressionUUID->"6b5e276e-e513-40c4-be12-e4346c3119b6"],

Cell[CellGroupData[{

Cell[BoxData[{
 RowBox[{
  RowBox[{"X", "=", 
   FractionBox["labor", "\[Beta]"]}], ";", 
  RowBox[{"c", "=", 
   SuperscriptBox["P", 
    RowBox[{"1", "-", "\[Beta]"}]]}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"eqns", "=", 
   RowBox[{"{", 
    RowBox[{
     RowBox[{
      RowBox[{
       RowBox[{
        FractionBox["X", 
         RowBox[{"\[Epsilon]", "*", 
          SuperscriptBox["P", 
           RowBox[{"1", "-", "\[Epsilon]"}]]}]], "*", 
        SuperscriptBox[
         RowBox[{"(", 
          RowBox[{
           FractionBox["\[Epsilon]", 
            RowBox[{"\[Epsilon]", "-", "1"}]], "c"}], ")"}], 
         RowBox[{"1", "-", "\[Epsilon]"}]], "*", 
        FractionBox["1", 
         RowBox[{"\[Theta]", "-", 
          RowBox[{"(", 
           RowBox[{"\[Epsilon]", "-", "1"}], ")"}]}]], "\[Theta]", " ", 
        SuperscriptBox["b", "\[Theta]"], 
        SuperscriptBox["aut", 
         RowBox[{"\[Theta]", "-", 
          RowBox[{"(", 
           RowBox[{"\[Epsilon]", "-", "1"}], ")"}]}]]}], "-", 
       RowBox[{
        SuperscriptBox[
         RowBox[{"(", 
          RowBox[{"b", "*", "aut"}], ")"}], "\[Theta]"], " ", "c", " ", 
        "fp"}]}], "\[Equal]", " ", 
      RowBox[{"c", " ", "fe"}]}], ",", 
     RowBox[{"aut", "\[Equal]", 
      RowBox[{
       FractionBox[
        RowBox[{"\[Epsilon]", "-", "1"}], "\[Epsilon]"], "*", 
       FractionBox["P", "c"], "*", 
       SuperscriptBox[
        RowBox[{"(", 
         FractionBox["X", 
          RowBox[{"\[Epsilon]", " ", "c", " ", "fp"}]], ")"}], 
        FractionBox["1", 
         RowBox[{"\[Epsilon]", "-", "1"}]]]}]}], ",", 
     RowBox[{"P", "\[Equal]", 
      RowBox[{
       FractionBox["1", "b"], "*", 
       SuperscriptBox[
        RowBox[{"(", 
         FractionBox["\[Theta]", 
          RowBox[{"\[Theta]", "-", 
           RowBox[{"(", 
            RowBox[{"\[Epsilon]", "-", "1"}], ")"}]}]], ")"}], 
        FractionBox[
         RowBox[{"-", "1"}], "\[Theta]"]], "*", 
       FractionBox["\[Epsilon]", 
        RowBox[{"\[Epsilon]", "-", "1"}]], "*", 
       SuperscriptBox[
        RowBox[{"(", 
         FractionBox["X", "\[Epsilon]"], ")"}], 
        RowBox[{"-", 
         RowBox[{"(", 
          FractionBox[
           RowBox[{"\[Theta]", "-", 
            RowBox[{"(", 
             RowBox[{"\[Epsilon]", "-", "1"}], ")"}]}], 
           RowBox[{"\[Theta]", " ", 
            RowBox[{"(", 
             RowBox[{"\[Epsilon]", "-", "1"}], ")"}]}]], ")"}]}]], "*", 
       SuperscriptBox[
        RowBox[{"(", 
         RowBox[{"Ibar", "*", 
          SuperscriptBox[
           RowBox[{"(", 
            FractionBox["1", "c"], ")"}], "\[Theta]"], "*", 
          SuperscriptBox[
           RowBox[{"(", 
            FractionBox["1", 
             RowBox[{"c", " ", "fp"}]], ")"}], 
           FractionBox[
            RowBox[{"\[Theta]", "-", 
             RowBox[{"(", 
              RowBox[{"\[Epsilon]", "-", "1"}], ")"}]}], 
            RowBox[{"\[Epsilon]", "-", "1"}]]]}], ")"}], 
        FractionBox[
         RowBox[{"-", "1"}], "\[Theta]"]]}]}]}], "}"}]}], ";", 
  RowBox[{"sol", "=", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{"aut", ",", "P", ",", "Ibar"}], "}"}], "/.", 
    RowBox[{"FindRoot", "[", 
     RowBox[{"eqns", ",", 
      RowBox[{"{", 
       RowBox[{
        RowBox[{"{", 
         RowBox[{"aut", ",", "6"}], "}"}], ",", 
        RowBox[{"{", 
         RowBox[{"P", ",", "0.5"}], "}"}], ",", 
        RowBox[{"{", 
         RowBox[{"Ibar", ",", 
          SuperscriptBox["10", "6"]}], "}"}]}], "}"}]}], "]"}]}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"pool", "=", 
    RowBox[{"Round", "[", 
     RowBox[{"sol", "[", 
      RowBox[{"[", "3", "]"}], "]"}], "]"}]}], ";"}], 
  "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{"Print", "[", 
  RowBox[{
  "\"\<Equilibrium (\!\(\*OverscriptBox[\(a\), \(_\)]\), P, \
\!\(\*OverscriptBox[\(I\), \(_\)]\)) = \>\"", ",", "sol"}], 
  "]"}], "\[IndentingNewLine]", 
 RowBox[{"Print", "[", 
  RowBox[{"\"\<Entry rate: \>\"", ",", 
   RowBox[{"1", "-", 
    RowBox[{"CDF", "[", 
     RowBox[{
      RowBox[{"ParetoDistribution", "[", 
       RowBox[{"b", ",", "\[Theta]"}], "]"}], ",", 
      FractionBox["1", 
       RowBox[{"sol", "[", 
        RowBox[{"[", "1", "]"}], "]"}]]}], "]"}]}]}], 
  "]"}], "\[IndentingNewLine]", 
 RowBox[{"Print", "[", 
  RowBox[{"\"\<Expected number of active firms: \>\"", ",", 
   RowBox[{"Round", "[", 
    RowBox[{
     RowBox[{"(", 
      RowBox[{"1", "-", 
       RowBox[{"CDF", "[", 
        RowBox[{
         RowBox[{"ParetoDistribution", "[", 
          RowBox[{"b", ",", "\[Theta]"}], "]"}], ",", 
         FractionBox["1", 
          RowBox[{"sol", "[", 
           RowBox[{"[", "1", "]"}], "]"}]]}], "]"}]}], ")"}], "*", 
     RowBox[{"sol", "[", 
      RowBox[{"[", "3", "]"}], "]"}]}], "]"}]}], "]"}]}], "Input",
 CellChangeTimes->{{3.647881265052437*^9, 3.647881268705659*^9}, 
   3.834844878756816*^9},
 CellLabel->"In[50]:=",ExpressionUUID->"59c952e5-2ffe-40c9-9f57-7cc8ae867078"],

Cell[CellGroupData[{

Cell[BoxData[
 InterpretationBox[
  RowBox[{"\<\"Equilibrium (\\!\\(\\*OverscriptBox[\\(a\\), \\(_\\)]\\), P, \
\\!\\(\\*OverscriptBox[\\(I\\), \\(_\\)]\\)) = \"\>", "\[InvisibleSpace]", 
   RowBox[{"{", 
    RowBox[{
    "6.584278383987207`", ",", "0.08615388190159085`", ",", 
     "4.608460778212577`*^7"}], "}"}]}],
  SequenceForm[
  "Equilibrium (\!\(\*OverscriptBox[\(a\), \(_\)]\), P, \
\!\(\*OverscriptBox[\(I\), \(_\)]\)) = ", {6.584278383987207, 
   0.08615388190159085, 4.608460778212577*^7}],
  Editable->False]], "Print",
 CellChangeTimes->{
  3.6478036389588947`*^9, 3.6478047796422586`*^9, 3.647806840028509*^9, 
   3.647879270161682*^9, 3.647880121922086*^9, 3.647881270922986*^9, 
   3.6478816666273937`*^9, 3.647883392043027*^9, 3.834843713026106*^9, 
   3.834844879533736*^9, 3.834845836006109*^9, 3.83484590665588*^9, 
   3.834846414158901*^9, {3.8348494132122974`*^9, 3.8348494638776755`*^9}, 
   3.8348624235988865`*^9},
 CellLabel->
  "During evaluation of \
In[50]:=",ExpressionUUID->"2181615a-8e65-4806-8521-bd1e80e1bf68"],

Cell[BoxData[
 InterpretationBox[
  RowBox[{"\<\"Entry rate: \"\>", "\[InvisibleSpace]", 
   "0.10916750342049841`"}],
  SequenceForm["Entry rate: ", 0.10916750342049841`],
  Editable->False]], "Print",
 CellChangeTimes->{
  3.6478036389588947`*^9, 3.6478047796422586`*^9, 3.647806840028509*^9, 
   3.647879270161682*^9, 3.647880121922086*^9, 3.647881270922986*^9, 
   3.6478816666273937`*^9, 3.647883392043027*^9, 3.834843713026106*^9, 
   3.834844879533736*^9, 3.834845836006109*^9, 3.83484590665588*^9, 
   3.834846414158901*^9, {3.8348494132122974`*^9, 3.8348494638776755`*^9}, 
   3.83486242360328*^9},
 CellLabel->
  "During evaluation of \
In[50]:=",ExpressionUUID->"2f15bb83-a2aa-48d6-a028-3ff383abccce"],

Cell[BoxData[
 InterpretationBox[
  RowBox[{"\<\"Expected number of active firms: \"\>", "\[InvisibleSpace]", 
   "5030942"}],
  SequenceForm["Expected number of active firms: ", 5030942],
  Editable->False]], "Print",
 CellChangeTimes->{
  3.6478036389588947`*^9, 3.6478047796422586`*^9, 3.647806840028509*^9, 
   3.647879270161682*^9, 3.647880121922086*^9, 3.647881270922986*^9, 
   3.6478816666273937`*^9, 3.647883392043027*^9, 3.834843713026106*^9, 
   3.834844879533736*^9, 3.834845836006109*^9, 3.83484590665588*^9, 
   3.834846414158901*^9, {3.8348494132122974`*^9, 3.8348494638776755`*^9}, 
   3.834862423605304*^9},
 CellLabel->
  "During evaluation of \
In[50]:=",ExpressionUUID->"28edc522-23bd-4949-91c7-466b9c008b5f"]
}, Open  ]]
}, Open  ]],

Cell[TextData[{
 "Simulations\n",
 StyleBox["Note: some functions go through a C-compiler to speed up \
computations. Furthermore, we allow for parallelization across cores.",
  FontSize->12,
  FontSlant->"Italic"]
}], "Text",
 CellChangeTimes->{{3.900753136963245*^9, 3.9007531875613375`*^9}, {
  3.9007591434605737`*^9, 3.9007591678001065`*^9}, {3.900759216308401*^9, 
  3.9007592373481183`*^9}},ExpressionUUID->"9635a935-23f3-47b6-a50d-\
f09a91885f3f"],

Cell[BoxData[{
 RowBox[{"SetDirectory", "[", "\"\<yourdirectory\\\\Simulations\>\"", 
  "]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"sim", "=", "5001"}], ";", 
  RowBox[{"print", "=", "10"}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"firmcount", "=", 
   RowBox[{"ConstantArray", "[", 
    RowBox[{"0", ",", 
     RowBox[{"{", "sim", "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"\[Alpha]grid", "=", 
   RowBox[{"Table", "[", 
    RowBox[{"x", ",", 
     RowBox[{"{", 
      RowBox[{"x", ",", "0.05", ",", "0.35", ",", "0.01"}], "}"}]}], "]"}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"\[Alpha]mat", "=", 
   RowBox[{"ConstantArray", "[", 
    RowBox[{"0", ",", 
     RowBox[{"{", 
      RowBox[{
       RowBox[{"Length", "[", "\[Alpha]grid", "]"}], ",", "sim"}], "}"}]}], 
    "]"}]}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"vol1", "=", 
   RowBox[{"ConstantArray", "[", 
    RowBox[{"0", ",", "sim"}], "]"}]}], ";"}]}], "Input",
 CellChangeTimes->{{3.6478063865857897`*^9, 3.647806391244417*^9}, {
  3.6478814248090773`*^9, 3.647881428822053*^9}, {3.647881604577908*^9, 
  3.647881606892467*^9}, {3.647881672777523*^9, 3.647881676806819*^9}, {
  3.647881763504733*^9, 3.647881771602189*^9}, {3.834843772055072*^9, 
  3.83484383909025*^9}, {3.8348463993746*^9, 3.83484640587626*^9}, {
  3.8348494680519314`*^9, 3.834849474146137*^9}, {3.8348624158486023`*^9, 
  3.834862415970822*^9}, {3.900753196285619*^9, 
  3.9007531998408957`*^9}},ExpressionUUID->"b7eb9c78-e005-4c74-89bf-\
93570061a4a2"],

Cell[BoxData[{
 RowBox[{
  RowBox[{
   RowBox[{"g", "=", 
    RowBox[{"Compile", "[", 
     RowBox[{
      RowBox[{"{", "x", "}"}], ",", "\[IndentingNewLine]", 
      RowBox[{"If", "[", 
       RowBox[{
        RowBox[{
         FractionBox["1", "x"], "\[LessEqual]", " ", 
         RowBox[{"sol", "[", 
          RowBox[{"[", "1", "]"}], "]"}]}], ",", 
        FractionBox["1", "x"], ",", "0"}], "]"}], ",", "\[IndentingNewLine]", 
      
      RowBox[{"CompilationTarget", "\[Rule]", " ", "\"\<C\>\""}], ",", 
      RowBox[{"\"\<RuntimeOptions\>\"", "\[Rule]", " ", "\"\<Speed\>\""}]}], 
     "\[IndentingNewLine]", "]"}]}], ";"}], 
  "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"salfunc", "=", 
    RowBox[{"Compile", "[", 
     RowBox[{
      RowBox[{"{", "x", "}"}], ",", "\[IndentingNewLine]", 
      RowBox[{
       FractionBox["X", 
        SuperscriptBox[
         RowBox[{"sol", "[", 
          RowBox[{"[", "2", "]"}], "]"}], 
         RowBox[{"1", "-", "\[Epsilon]"}]]], "*", 
       SuperscriptBox[
        RowBox[{"(", 
         RowBox[{
          FractionBox["\[Epsilon]", 
           RowBox[{"\[Epsilon]", "-", "1"}]], "*", 
          SuperscriptBox[
           RowBox[{"sol", "[", 
            RowBox[{"[", "2", "]"}], "]"}], 
           RowBox[{"1", "-", "\[Beta]"}]], "*", "x"}], ")"}], 
        RowBox[{"1", "-", "\[Epsilon]"}]]}], ",", "\[IndentingNewLine]", 
      RowBox[{"CompilationTarget", "\[Rule]", " ", "\"\<C\>\""}], ",", 
      RowBox[{"\"\<RuntimeOptions\>\"", "\[Rule]", " ", "\"\<Speed\>\""}]}], 
     "\[IndentingNewLine]", "]"}]}], ";"}], 
  "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"f1", "=", 
    RowBox[{"Compile", "[", 
     RowBox[{
      RowBox[{"{", "x", "}"}], ",", "\[IndentingNewLine]", 
      SuperscriptBox["x", "2"], ",", "\[IndentingNewLine]", 
      RowBox[{"CompilationTarget", "\[Rule]", " ", "\"\<C\>\""}], ",", 
      RowBox[{"\"\<RuntimeOptions\>\"", "\[Rule]", " ", "\"\<Speed\>\""}]}], 
     "\[IndentingNewLine]", "]"}]}], ";"}], 
  "\[IndentingNewLine]"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"f2", "=", 
   RowBox[{"Compile", "[", 
    RowBox[{
     RowBox[{"{", 
      RowBox[{"x", ",", "\[Alpha]"}], "}"}], ",", "\[IndentingNewLine]", 
     SuperscriptBox[
      RowBox[{"(", 
       RowBox[{
        SuperscriptBox["x", 
         RowBox[{"-", "\[Alpha]"}]], "*", "x"}], ")"}], "2"], ",", 
     "\[IndentingNewLine]", 
     RowBox[{"CompilationTarget", "\[Rule]", " ", "\"\<C\>\""}], ",", 
     RowBox[{"\"\<RuntimeOptions\>\"", "\[Rule]", " ", "\"\<Speed\>\""}]}], 
    "\[IndentingNewLine]", "]"}]}], ";"}]}], "Input",
 CellChangeTimes->{{3.647805154556774*^9, 3.647805214236772*^9}, {
   3.6478052827771683`*^9, 3.6478053314860563`*^9}, {3.6478054257313013`*^9, 
   3.647805426315442*^9}, 3.6478055583510447`*^9, {3.647805782204472*^9, 
   3.647805794077902*^9}, {3.647806321765523*^9, 3.64780633493102*^9}, {
   3.647806404033992*^9, 3.6478064576873426`*^9}, {3.6478064911461897`*^9, 
   3.647806511394166*^9}, {3.647806731506879*^9, 3.647806816141924*^9}, 
   3.647806918196039*^9, {3.64788131122682*^9, 3.6478813177232237`*^9}, {
   3.64788148214893*^9, 3.647881522248246*^9}, {3.647881616262993*^9, 
   3.647881624475213*^9}, {3.647881692950448*^9, 3.647881716764654*^9}, {
   3.647881749917552*^9, 3.647881755312304*^9}, {3.647883340454996*^9, 
   3.647883354969762*^9}, 3.834843627241553*^9, {3.8348448613008623`*^9, 
   3.8348448623560395`*^9}},
 CellLabel->"In[62]:=",ExpressionUUID->"0d511d06-2d43-40cf-901e-f8cf4a663905"],

Cell[BoxData[{
 RowBox[{
  RowBox[{"SetSharedVariable", "[", 
   RowBox[{"vol1", ",", "\[Alpha]mat"}], "]"}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"ParallelDo", "[", "\[IndentingNewLine]", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{
      RowBox[{"adraws", "=", 
       RowBox[{"RandomVariate", "[", 
        RowBox[{
         RowBox[{"ParetoDistribution", "[", 
          RowBox[{"b", ",", "\[Theta]"}], "]"}], ",", "pool"}], "]"}]}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"active", "=", 
       RowBox[{"Map", "[", 
        RowBox[{"g", ",", "adraws"}], "]"}]}], ";", "\[IndentingNewLine]", 
      "\[IndentingNewLine]", 
      RowBox[{"ashocks", "=", 
       RowBox[{"DeleteCases", "[", 
        RowBox[{"active", ",", "0."}], "]"}]}], ";", "\[IndentingNewLine]", 
      RowBox[{"firms", "=", 
       RowBox[{"Length", "[", "ashocks", "]"}]}], ";", "\[IndentingNewLine]", 
      
      RowBox[{
       RowBox[{"firmcount", "[", 
        RowBox[{"[", "s", "]"}], "]"}], "=", "firms"}], ";", 
      "\[IndentingNewLine]", "\[IndentingNewLine]", 
      RowBox[{"sales", "=", 
       RowBox[{"Map", "[", 
        RowBox[{"salfunc", ",", "ashocks"}], "]"}]}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"totalsales", "=", 
       RowBox[{"sales", ".", 
        RowBox[{"ConstantArray", "[", 
         RowBox[{"1", ",", "firms"}], "]"}]}]}], ";", "\[IndentingNewLine]", 
      "\[IndentingNewLine]", 
      RowBox[{"DumpSave", "[", 
       RowBox[{
        RowBox[{"\"\<sales_\>\"", "<>", 
         RowBox[{"ToString", "[", "s", "]"}], "<>", "\"\<_sim.dat\>\""}], ",",
         "sales"}], "]"}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
      
      RowBox[{
       RowBox[{"vol1", "[", 
        RowBox[{"[", "s", "]"}], "]"}], "=", 
       RowBox[{"\[Sigma]", "*", 
        RowBox[{"Sqrt", "[", 
         FractionBox[
          RowBox[{
           RowBox[{"Map", "[", 
            RowBox[{"f1", ",", "sales"}], "]"}], ".", 
           RowBox[{"ConstantArray", "[", 
            RowBox[{"1", ",", "firms"}], "]"}]}], 
          SuperscriptBox["totalsales", "2"]], "]"}]}]}], ";", 
      "\[IndentingNewLine]", 
      RowBox[{"Do", "[", "\[IndentingNewLine]", 
       RowBox[{
        RowBox[{
         RowBox[{"\[Alpha]", "=", 
          RowBox[{"\[Alpha]grid", "[", 
           RowBox[{"[", "i", "]"}], "]"}]}], ";", "\[IndentingNewLine]", 
         RowBox[{
          RowBox[{"\[Alpha]mat", "[", 
           RowBox[{"[", 
            RowBox[{"i", ",", "s"}], "]"}], "]"}], "=", 
          RowBox[{"A", "*", 
           RowBox[{"Sqrt", "[", 
            FractionBox[
             RowBox[{
              RowBox[{"Map", "[", 
               RowBox[{
                RowBox[{
                 RowBox[{"f2", "[", 
                  RowBox[{"#", ",", "\[Alpha]"}], "]"}], "&"}], ",", 
                "sales"}], "]"}], ".", 
              RowBox[{"ConstantArray", "[", 
               RowBox[{"1", ",", "firms"}], "]"}]}], 
             SuperscriptBox["totalsales", "2"]], "]"}]}]}], ";"}], 
        "\[IndentingNewLine]", ",", 
        RowBox[{"{", 
         RowBox[{"i", ",", "1", ",", 
          RowBox[{"Length", "[", "\[Alpha]grid", "]"}]}], "}"}]}], 
       "\[IndentingNewLine]", "]"}], ";"}], "\[IndentingNewLine]", ",", 
     RowBox[{"{", 
      RowBox[{"s", ",", "1", ",", "sim"}], "}"}]}], "\[IndentingNewLine]", 
    "\[IndentingNewLine]", "]"}], ";"}], "//", "AbsoluteTiming"}]}], "Input",
 CellChangeTimes->{{3.834847144732654*^9, 3.8348471489134417`*^9}, {
  3.8348475950923195`*^9, 3.834847605116162*^9}},
 CellLabel->"In[66]:=",ExpressionUUID->"f019731a-82d7-41ea-adaa-be43049927a2"],

Cell["\<\
Fourth line of Table II - baseline case in which Gibrat\[CloseCurlyQuote]s \
law holds\
\>", "Text",
 CellChangeTimes->{{3.900753136963245*^9, 3.9007531655342536`*^9}, {
  3.900753308627946*^9, 3.90075332060773*^9}, {3.9007592735104823`*^9, 
  3.9007592773067617`*^9}},ExpressionUUID->"258bbbb5-f05a-43bb-8178-\
fea79b9e37e3"],

Cell[BoxData[
 RowBox[{"Median", "[", 
  FractionBox["vol1", "USvol"], "]"}]], "Input",
 CellChangeTimes->{{3.8348463665040216`*^9, 3.834846378893588*^9}, {
  3.834846878546316*^9, 3.8348469508150797`*^9}, {3.834846996353786*^9, 
  3.834847042248167*^9}, {3.8348470885942893`*^9, 3.8348471234731693`*^9}, {
  3.8348620756008525`*^9, 3.8348620763231077`*^9}, {3.8348621443600445`*^9, 
  3.8348621609181023`*^9}, {3.834862332368167*^9, 3.834862350070386*^9}, {
  3.8349201741158824`*^9, 3.8349202065774918`*^9}, {3.8371705637983265`*^9, 
  3.837170564217566*^9}, {3.9007533253493423`*^9, 
  3.9007533339498625`*^9}},ExpressionUUID->"e5b2149f-6d26-43c7-b472-\
bcc5baf8d0a3"],

Cell[BoxData[{
 RowBox[{
  RowBox[{"Export", "[", 
   RowBox[{"\"\<gibrat.csv\>\"", ",", "vol1", ",", "\"\<Table\>\""}], "]"}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"Export", "[", 
   RowBox[{"\"\<sizevar.csv\>\"", ",", "\[Alpha]mat", ",", "\"\<Table\>\""}], 
   "]"}], ";"}]}], "Input",
 CellChangeTimes->{{3.8348498403741255`*^9, 3.834849844633728*^9}, {
  3.9007592823382287`*^9, 
  3.90075928355474*^9}},ExpressionUUID->"e7c19eaa-8dd6-4e21-b3a1-\
d31dbc312026"],

Cell["\<\
Cases in which Gibrat\[CloseCurlyQuote]s law is violated: size-variance \
relationship\
\>", "Text",
 CellChangeTimes->{{3.900753136963245*^9, 3.9007531655342536`*^9}, {
  3.900753308627946*^9, 3.90075332060773*^9}, {3.9007592735104823`*^9, 
  3.9007592773067617`*^9}, {3.90075975600707*^9, 
  3.900759770753617*^9}},ExpressionUUID->"423a478a-e4a5-4dbe-86d9-\
db4883f32662"],

Cell[BoxData[{
 RowBox[{
  RowBox[{"\[Alpha]mat", "=", 
   RowBox[{"Import", "[", 
    RowBox[{"\"\<sizevar.csv\>\"", ",", "\"\<Table\>\""}], "]"}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"vol1", "=", 
   RowBox[{"Import", "[", 
    RowBox[{"\"\<gibrat.csv\>\"", ",", "\"\<Table\>\""}], "]"}]}], 
  ";"}]}], "Input",
 CellChangeTimes->{{3.837164788426132*^9, 3.837164807909978*^9}, {
  3.8371648391165686`*^9, 3.8371648406169434`*^9}, {3.83716723093703*^9, 
  3.837167238931517*^9}},
 CellLabel->
  "In[248]:=",ExpressionUUID->"a69262d2-6bf4-4960-94f1-ea3dd3c0df9f"],

Cell[BoxData[{
 RowBox[{
  RowBox[{"\[Alpha]gridspecial", "=", 
   RowBox[{"{", 
    RowBox[{"0.2149", ",", "0.2494", ",", "0.3942", ",", "0.4327"}], "}"}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"\[Alpha]matspecial", "=", 
   RowBox[{"ConstantArray", "[", 
    RowBox[{"0", ",", 
     RowBox[{"{", 
      RowBox[{
       RowBox[{"Length", "[", "\[Alpha]gridspecial", "]"}], ",", "sim"}], 
      "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"SetSharedVariable", "[", "\[Alpha]matspecial", "]"}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{
   RowBox[{"ParallelDo", "[", "\[IndentingNewLine]", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{
      RowBox[{"Get", "[", 
       RowBox[{"\"\<sales_\>\"", "<>", 
        RowBox[{"ToString", "[", "s", "]"}], "<>", "\"\<_sim.dat\>\""}], 
       "]"}], ";", "\[IndentingNewLine]", 
      RowBox[{"firms", "=", 
       RowBox[{"Length", "[", "sales", "]"}]}], ";", "\[IndentingNewLine]", 
      RowBox[{"totalsales", "=", 
       RowBox[{"sales", ".", 
        RowBox[{"ConstantArray", "[", 
         RowBox[{"1", ",", "firms"}], "]"}]}]}], ";", "\[IndentingNewLine]", 
      RowBox[{"Do", "[", "\[IndentingNewLine]", 
       RowBox[{
        RowBox[{
         RowBox[{"\[Alpha]", "=", 
          RowBox[{"\[Alpha]gridspecial", "[", 
           RowBox[{"[", "i", "]"}], "]"}]}], ";", "\[IndentingNewLine]", 
         RowBox[{
          RowBox[{"\[Alpha]matspecial", "[", 
           RowBox[{"[", 
            RowBox[{"i", ",", "s"}], "]"}], "]"}], "=", 
          RowBox[{"A", "*", 
           RowBox[{"Sqrt", "[", 
            RowBox[{
             RowBox[{
              RowBox[{"Map", "[", 
               RowBox[{
                RowBox[{
                 RowBox[{"f2", "[", 
                  RowBox[{"#", ",", "\[Alpha]"}], "]"}], "&"}], ",", 
                "sales"}], "]"}], ".", 
              RowBox[{"ConstantArray", "[", 
               RowBox[{"1", ",", "firms"}], "]"}]}], "/", 
             SuperscriptBox["totalsales", "2"]}], "]"}]}]}], ";"}], 
        "\[IndentingNewLine]", ",", 
        RowBox[{"{", 
         RowBox[{"i", ",", "1", ",", 
          RowBox[{"Length", "[", "\[Alpha]gridspecial", "]"}]}], "}"}]}], 
       "\[IndentingNewLine]", "]"}], ";"}], "\[IndentingNewLine]", ",", 
     RowBox[{"{", 
      RowBox[{"s", ",", "1", ",", "sim"}], "}"}]}], "\[IndentingNewLine]", 
    "\[IndentingNewLine]", "\[IndentingNewLine]", "]"}], ";"}], "//", 
  "AbsoluteTiming"}]}], "Input",
 CellChangeTimes->{{3.8350906585297127`*^9, 3.835090700178257*^9}, {
  3.8350908221072216`*^9, 3.835090901518704*^9}, {3.8350909732026424`*^9, 
  3.8350909880986867`*^9}, {3.835091031999861*^9, 3.835091107065672*^9}, {
  3.835091282461709*^9, 3.835091282786089*^9}, {3.900759448771371*^9, 
  3.900759451568567*^9}},ExpressionUUID->"3e828b10-0301-4d63-97c8-\
72a5fa101367"],

Cell[BoxData[{
 RowBox[{
  RowBox[{"medspecial", "=", 
   RowBox[{"ConstantArray", "[", 
    RowBox[{"0", ",", 
     RowBox[{"Length", "[", "\[Alpha]gridspecial", "]"}]}], "]"}]}], 
  ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"For", "[", 
   RowBox[{
    RowBox[{"i", "=", "1"}], ",", 
    RowBox[{"i", "\[LessEqual]", " ", 
     RowBox[{"Length", "[", "\[Alpha]gridspecial", "]"}]}], ",", 
    RowBox[{"i", "++"}], ",", "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{
      RowBox[{"medspecial", "[", 
       RowBox[{"[", "i", "]"}], "]"}], "=", 
      RowBox[{"Median", "[", 
       FractionBox[
        RowBox[{"\[Alpha]matspecial", "[", 
         RowBox[{"[", 
          RowBox[{"i", ",", "All"}], "]"}], "]"}], "USvol"], "]"}]}], ";"}]}],
    "\[IndentingNewLine]", "]"}], ";"}], "\[IndentingNewLine]", 
 RowBox[{"\[Alpha]gridspecial", "//", "TableForm"}]}], "Input",
 CellChangeTimes->{{3.835091116007047*^9, 3.835091276605069*^9}, {
  3.835097950898859*^9, 3.835097998285708*^9}, {3.9007538865735607`*^9, 
  3.9007538869402447`*^9}},ExpressionUUID->"a9e05ed0-abc2-4357-88d3-\
f0c63a3e922d"],

Cell["\<\
First line of Table II - Size-variance relationship (constant : calibrated)\
\>", "Text",
 CellChangeTimes->{{3.900753136963245*^9, 3.9007531655342536`*^9}, {
  3.900753308627946*^9, 3.90075332060773*^9}, {3.900754000411828*^9, 
  3.9007540491370783`*^9}},ExpressionUUID->"42c0a25e-0317-4706-9b38-\
376479c08c72"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"medspecial", "[", 
    RowBox[{"[", 
     RowBox[{"1", ";;", "2"}], "]"}], "]"}], "*", "1.4"}], "//", 
  "TableForm"}]], "Input",
 CellChangeTimes->{{3.9007539024559197`*^9, 3.900753912997526*^9}, {
  3.90075481198569*^9, 
  3.900754814893*^9}},ExpressionUUID->"4be8d20e-ee75-4764-b063-9933a8bc7035"],

Cell["\<\
Second line of Table II - Size-variance relationship (constant : estimated \
values from first and third column of Table 1)\
\>", "Text",
 CellChangeTimes->{{3.900753136963245*^9, 3.9007531655342536`*^9}, {
  3.900753308627946*^9, 3.90075332060773*^9}, {3.900754000411828*^9, 
  3.900754063369774*^9}, {3.9007594979304295`*^9, 
  3.9007595221769943`*^9}},ExpressionUUID->"65090272-13cd-4135-974e-\
c6012fafc7ce"],

Cell[BoxData[{
 RowBox[{
  RowBox[{"medspecial", "[", 
   RowBox[{"[", "1", "]"}], "]"}], "*", 
  RowBox[{"(", 
   RowBox[{"Exp", "[", 
    RowBox[{"-", "0.6063"}], "]"}], ")"}]}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"medspecial", "[", 
   RowBox[{"[", "2", "]"}], "]"}], "*", 
  RowBox[{"(", 
   RowBox[{"Exp", "[", 
    RowBox[{"-", "0.6166"}], "]"}], ")"}]}]}], "Input",
 CellChangeTimes->{{3.9007539024559197`*^9, 3.900753912997526*^9}, {
  3.90075408149069*^9, 3.9007541302731085`*^9}, {3.9007547942180185`*^9, 
  3.900754803250002*^9}, {3.9007594730877666`*^9, 
  3.9007594860679703`*^9}},ExpressionUUID->"83b84326-807a-4100-81cc-\
e8ac1a286f95"],

Cell["\<\
Third line of Table II - Size-variance relationship (constant : inferred from \
elasticity based on sample of large firms only and non-parametric \
regressions; large firm has 100,000 employees in these calculations)\
\>", "Text",
 CellChangeTimes->{{3.900753136963245*^9, 3.9007531655342536`*^9}, {
  3.900753308627946*^9, 3.90075332060773*^9}, {3.900754000411828*^9, 
  3.900754063369774*^9}, {3.900758867043135*^9, 3.9007588830637274`*^9}, {
  3.9007589513038893`*^9, 3.900758952831502*^9}, {3.9007590454853373`*^9, 
  3.9007590508805175`*^9}, {3.900759542339525*^9, 3.900759574698143*^9}, {
  3.900759816777711*^9, 
  3.900759821307233*^9}},ExpressionUUID->"c9d675b2-7155-4ccf-8efd-\
d4f2a6f25a3b"],

Cell[BoxData[{
 RowBox[{
  RowBox[{"(", 
   RowBox[{
    RowBox[{"Exp", "[", 
     RowBox[{
      RowBox[{"-", "1.592"}], "-", "0.8341"}], "]"}], "/", 
    SuperscriptBox["100000", 
     RowBox[{"-", "0.3942"}]]}], ")"}], "*", 
  RowBox[{"medspecial", "[", 
   RowBox[{"[", "3", "]"}], "]"}]}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"(", 
   RowBox[{
    RowBox[{"Exp", "[", 
     RowBox[{
      RowBox[{"-", "1.592"}], "-", "0.8341"}], "]"}], "/", 
    SuperscriptBox["100000", 
     RowBox[{"-", "0.4327"}]]}], ")"}], "*", 
  RowBox[{"medspecial", "[", 
   RowBox[{"[", "4", "]"}], "]"}]}]}], "Input",
 CellChangeTimes->{{3.900754828711356*^9, 3.90075483261933*^9}, {
  3.9007590092302265`*^9, 3.900759029119813*^9}, {3.9007590666410856`*^9, 
  3.900759095524973*^9}},ExpressionUUID->"e948d28b-3a0c-40a0-b0d6-\
58cbd3ab36cc"]
},
WindowSize->{949, 467},
WindowMargins->{{0, Automatic}, {Automatic, 0}},
ShowSelection->True,
TaggingRules->{
 "WelcomeScreenSettings" -> {"FEStarting" -> False}, "TryRealOnly" -> False},
Magnification:>0.75 Inherited,
FrontEndVersion->"13.1 for Microsoft Windows (64-bit) (August 22, 2022)",
StyleDefinitions->"Default.nb",
ExpressionUUID->"ecad051a-f28a-4ad5-9d3c-d3374f7f348d"
]
(* End of Notebook Content *)

(* Internal cache information *)
(*CellTagsOutline
CellTagsIndex->{}
*)
(*CellTagsIndex
CellTagsIndex->{}
*)
(*NotebookFileOutline
Notebook[{
Cell[557, 20, 223, 3, 21, "Input",ExpressionUUID->"f25518a5-68eb-46fb-9d4c-b9b0fd448da3"],
Cell[783, 25, 225, 5, 27, "Text",ExpressionUUID->"8f344a9c-ebb5-49d0-98ba-081018caf2bf"],
Cell[1011, 32, 1074, 30, 122, "Input",ExpressionUUID->"6b5e276e-e513-40c4-be12-e4346c3119b6"],
Cell[CellGroupData[{
Cell[2110, 66, 5050, 148, 253, "Input",ExpressionUUID->"59c952e5-2ffe-40c9-9f57-7cc8ae867078"],
Cell[CellGroupData[{
Cell[7185, 218, 1047, 22, 20, "Print",ExpressionUUID->"2181615a-8e65-4806-8521-bd1e80e1bf68"],
Cell[8235, 242, 712, 15, 18, "Print",ExpressionUUID->"2f15bb83-a2aa-48d6-a028-3ff383abccce"],
Cell[8950, 259, 729, 15, 18, "Print",ExpressionUUID->"28edc522-23bd-4949-91c7-466b9c008b5f"]
}, Open  ]]
}, Open  ]],
Cell[9706, 278, 455, 10, 41, "Text",ExpressionUUID->"9635a935-23f3-47b6-a50d-f09a91885f3f"],
Cell[10164, 290, 1564, 38, 93, "Input",ExpressionUUID->"b7eb9c78-e005-4c74-89bf-93570061a4a2"],
Cell[11731, 330, 3582, 83, 351, "Input",ExpressionUUID->"0d511d06-2d43-40cf-901e-f8cf4a663905"],
Cell[15316, 415, 3659, 91, 411, "Input",ExpressionUUID->"f019731a-82d7-41ea-adaa-be43049927a2"],
Cell[18978, 508, 336, 7, 27, "Text",ExpressionUUID->"258bbbb5-f05a-43bb-8178-fea79b9e37e3"],
Cell[19317, 517, 671, 11, 36, "Input",ExpressionUUID->"e5b2149f-6d26-43c7-b472-bcc5baf8d0a3"],
Cell[19991, 530, 482, 12, 36, "Input",ExpressionUUID->"e7c19eaa-8dd6-4e21-b3a1-d31dbc312026"],
Cell[20476, 544, 384, 8, 27, "Text",ExpressionUUID->"423a478a-e4a5-4dbe-86d9-db4883f32662"],
Cell[20863, 554, 583, 15, 36, "Input",ExpressionUUID->"a69262d2-6bf4-4960-94f1-ea3dd3c0df9f"],
Cell[21449, 571, 2876, 69, 261, "Input",ExpressionUUID->"3e828b10-0301-4d63-97c8-72a5fa101367"],
Cell[24328, 642, 1109, 28, 108, "Input",ExpressionUUID->"a9e05ed0-abc2-4357-88d3-f0c63a3e922d"],
Cell[25440, 672, 323, 6, 27, "Text",ExpressionUUID->"42c0a25e-0317-4706-9b38-376479c08c72"],
Cell[25766, 680, 347, 9, 21, "Input",ExpressionUUID->"4be8d20e-ee75-4764-b063-9933a8bc7035"],
Cell[26116, 691, 422, 8, 27, "Text",ExpressionUUID->"65090272-13cd-4135-974e-c6012fafc7ce"],
Cell[26541, 701, 660, 17, 36, "Input",ExpressionUUID->"83b84326-807a-4100-81cc-e8ac1a286f95"],
Cell[27204, 720, 712, 12, 45, "Text",ExpressionUUID->"c9d675b2-7155-4ccf-8efd-d4f2a6f25a3b"],
Cell[27919, 734, 833, 24, 41, "Input",ExpressionUUID->"e948d28b-3a0c-40a0-b0d6-58cbd3ab36cc"]
}
]
*)

